﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using FDEE.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEEPublicFunc
{
    public static class Common
    {
        /// <summary>
        /// 承台墩高说明
        /// </summary>
        private static Dictionary<string, int> _ListFootingDes = new Dictionary<string, int>() {
            {"FPME41",5 },{"FPME42",5 },{"FPTM3S",5 },{"F1PTM2",5 },{"FPTE2",5 },{"FPTE3",5 },{"FPTE32",5 },{"FPME11",5 },{"FPME12",5 },{"FPTM2U",5 },{"FPTM4",5 },
            {"F2PTM2",5 },{"F1PTM3",5 },{"F2PTM3",5 },{"FPM1",5 },
            {"FPTM1",5 },{"FPTM1S",5 },{"F1PTM1U",5 },{"F2PTM1U",5 },
            {"F1PTM1T",5 },{"F2PTM1T",5 }, {"F1PTM2T",5 },{"F2PTM2T",5 },{"FPTL2",5 },
            {"F1PTM3T",5 },{"F2PTM3T",5 },{"FPE11",3 },{"FPE12",3 },{"FPE13",3 },
            {"FPE14",3 },{"FPE21A",3 },{"FPE21B",3 },{"FPE22",3 },{"FPE31",3 },
            {"FPE32",3 },{"CPTM1",5 },{"CPTM1S",5 },{"CPTM2",5 },{"CPTM2S",5 },{"CPTM3U",5 },{"CPTM3S",5 },
            {"CPME41",5 },{"CPME42",5 },{"CPE12",3 },{"CPE21",3 },{"CPE22",3 },{"CPE32",3 },           
        };
        public static ObjectId CreateLayout(Database db, string layoutName, Extents2d ext, 
        double scale, int pdfNum = 1, bool isA1 = false,string tkPath="")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayout(layoutName, fsdTK, isA1, 1);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);

                double viwer1Width = ext.W() / scale;
                double viwer1Height = ext.L() / scale;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB),
             ext.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    MarginB + h * 0.5),
                    ext.GetCenter(), (int)scale);
                trans.Commit();
            }
            return ArrangeLayoutId;
        }

        public static ObjectId CreateLayoutNew(Database db, string layoutName, Extents2d ext,
 double scale, int pdfNum = 1,int pageNo=0,int divNo=0, bool isA1 = false, string tkPath = "")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo,divNo);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);

                double viwer1Width = ext.W() / scale;
                double viwer1Height = ext.L() / scale;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    MarginB + h * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    ext.GetCenter(), (int)scale);
                trans.Commit();
            }
            return ArrangeLayoutId;
        }

        public static ObjectId CreatePileCoorLayout(Database db, string layoutName, Extents2d extCT, Extents2d extN, List<Point2d> ptLst,
double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {

                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);
                double hCT = extCT.L() / scale;
                double viwerCTWidth = extCT.W() / scale;
                double viwerNWidth = extN.W() / scale;
                double wCT = viwerCTWidth;
                double wN =  viwerNWidth ;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             extCT.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, extCT.W() / scale + 3, extCT.L() / scale + 3,
                    Point2d.Origin.Convert2D(wCT*0.5+5 + MarginL,
                    MarginB+ hCT * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    extCT.GetCenter(), (int)scale);

                CreateViewport(db, ArrangeLayoutId, extN.W() / scale + 3, extN.L() / scale + 3,
                   Point2d.Origin.Convert2D(wCT+10 + wN *0.5 + MarginL,
                   MarginB + 10 + hCT * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                   extN.GetCenter(), (int)scale);

                Line lin1=    new Line(ptLst[0].Convert3D(), ptLst[1].Convert3D());
                //Line lin2 = new Line(ptLst[0].Convert3D(), ptLst[3].Convert3D());   
                double vw = lin1.Length;
                //double vh = lin2.Length ;
                CreateViewportByPointList(db, ArrangeLayoutId, vw+30, h - hCT+2, ptLst, Point2d.Origin.Convert2D(MarginL +w*0.5,
                    MarginB + hCT -5+(h-hCT)*0.5).Convert2D(420 * divNo, -297 * pageNo),1);
                trans.Commit();
            }
            return ArrangeLayoutId;
        }
        public static ObjectId CreateLayoutBylist(Database db, string layoutName, List<Dictionary<Extents2d, Extents2d>> dics,
 double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 20;
                double MarginT = 15;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);
                int num = dics.Count;
                double viwer1Width = w-3;
                double viwer1Height = h/ num;
                Extents2d ext = new Extents2d(); ;
                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter(), (int)scale);
                #endregion
                for (int i = 0; i < num; i++)
                {
                    Extents2d ext1 = dics[i].Keys.ToList()[0];
                    Extents2d ext2 = dics[i].Values.ToList()[0];
                    CreateViewport(db, ArrangeLayoutId, ext1.W() + 3, ext1.L()+2,
                        Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                        MarginB + viwer1Height * 0.70+(num-1-i)* viwer1Height).Convert2D(420 * divNo, -297 * pageNo),
                        ext1.GetCenter(), (int)scale);
                    CreateViewport(db, ArrangeLayoutId, ext2.W() + 3, ext2.L() +2,
                        Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                        MarginB + viwer1Height * 0.2 + (num - 1 - i) * viwer1Height).Convert2D(420 * divNo, -297 * pageNo),
                        ext2.GetCenter(), (int)scale);
                }
                trans.Commit();
            }
            return ArrangeLayoutId;
        }
        public static ObjectId CreateRLayoutNew(Database db, string layoutName, Extents2d ext,
double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);

                double viwer1Width = ext.W() / scale;
                double viwer1Height = ext.L() / scale;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(viwer1Width * 0.5 + MarginL+3,
                    MarginB + (h-30) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    ext.GetCenter(), (int)scale);
                trans.Commit();
            }
            return ArrangeLayoutId;
        }
        public static ObjectId CreateCRLayoutNew(Database db, string layoutName, Extents2d ext,
double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);

                double viwer1Width = ext.W() / scale;
                double viwer1Height = ext.L() / scale;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(viwer1Width * 0.5 + MarginL + 3,
                    MarginB + (h-5 )* 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    ext.GetCenter(), (int)scale);
                trans.Commit();
            }
            return ArrangeLayoutId;
        }
        public static ObjectId CreateBeamPLayoutNew(Database db, string layoutName, Extents2d ext,
 double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);

                double viwer1Width = ext.W() / scale;
                double viwer1Height = ext.L() / scale;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(viwer1Width * 0.5 + MarginL+3,
                    MarginB + (h-10) * 0.5+5).Convert2D(420 * divNo, -297 * pageNo),
                    ext.GetCenter(), (int)scale);
                trans.Commit();
            }
            return ArrangeLayoutId;
        }
        public static ObjectId CreateRebarLayoutNew(Database db, string layoutName, Extents2d ext,
double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {

            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);

                double viwer1Width = ext.W() / scale;
                double viwer1Height = ext.L() / scale;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    MarginB + h * 0.4).Convert2D(420 * divNo, -297 * pageNo),
                    ext.GetCenter(), (int)scale);
                trans.Commit();
            }
            return ArrangeLayoutId;
        }

        public static ObjectId CreateBufferLayout(Database db, string layoutName, Extents2d ext, List<Extents2d> exts, List<Extents2d> extMs,
double scale, int pdfNum = 1, int pageNo = 0,bool isA1 = false,string tkPath = "")
        {
            pdfNum = 1 + exts.Count;
            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, 0);

                double MarginL = 20;
                double MarginR = 5;
                double MarginB = 25;
                double MarginT = 7;

                double w = (420 - MarginL - MarginR);
                double h = (297 - MarginB - MarginT);

                double viwer1Width = ext.W() / scale;
                double viwer1Height = ext.L() / scale;

                #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(0, -297 * pageNo),
             ext.GetCenter(), (int)scale);
                #endregion

                CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    MarginB + h * 0.5).Convert2D(0, -297 * pageNo),
                    ext.GetCenter(), (int)scale);

                if(exts.Count>0)
                {
                    for(int i=0;i< exts.Count;i++)
                    {
                        viwer1Width = exts[i].W() / scale;
                        viwer1Height = exts[i].L() / scale;
                       double  viwer2Height = extMs[i].L() / scale;
                        double viwer2Width = extMs[i].W() / scale;
                        #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                        CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                     Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * (i+1), -297 * pageNo),
                      exts[i].GetCenter(), (int)scale);
                        #endregion
                        double h1 = h * (viwer1Height) / (viwer1Height + viwer2Height);
                        double h2 = h * (viwer2Height) / (viwer1Height + viwer2Height);
                        CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                            Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                            MarginB + h2+h1*0.5).Convert2D(420 * (i + 1), -297 * pageNo),
                             exts[i].GetCenter(), (int)scale);

                        CreateViewport(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                           Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                           MarginB + h2 * 0.5).Convert2D(420 * (i + 1), -297 * pageNo),
                            extMs[i].GetCenter(), (int)scale);
                    }
                }
                trans.Commit();
            }
            return ArrangeLayoutId;
        }

        public static void CreateIBufferLayout(Database db, string layoutName, List<Tuple<int, List<Extents2d>>> tuples,double scale,string tkPath="")
        {
            int pdfNum = 1;
            bool isA1 = false;
            pdfNum = 1;
         
            ObjectId ArrangeLayoutId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
                if (File.Exists(tkPath))
                    fsdTK = tkPath;
                for (int i = 0; i < tuples.Count; i++)
                {
                    List<Extents2d> lst = tuples[i].Item2;
                    if (tuples[i].Item2.Count>2)
                        pdfNum = 2;
                    else
                        pdfNum = 1; ;
                    ArrangeLayoutId = db.CreatMultLayout(layoutName, fsdTK, isA1, pdfNum, i);

                    double MarginL = 20;
                    double MarginR = 5;
                    double MarginB = 25;
                    double MarginT = 7;

                    double w = (420 - MarginL - MarginR);
                    double h = (297 - MarginB - MarginT);

                    if (pdfNum == 1)
                    {
                        if (lst.Count == 2)
                        {
                            double viwer1Width = lst[0].W() / scale;
                            double viwer1Height = lst[0].L() / scale;
                            double viwer2Height = lst[1].L() / scale;
                            double viwer2Width = lst[1].W() / scale;
                            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                         Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(0, -297 * i),
                          lst[0].GetCenter(), (int)scale);
                            #endregion
                            double h1 = h * (viwer1Height) / (viwer1Height + viwer2Height);
                            double h2 = h * (viwer2Height) / (viwer1Height + viwer2Height);
                            CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                                MarginB + h2 + h1 * 0.5).Convert2D(0, -297 * i),
                                 lst[0].GetCenter(), (int)scale);

                            CreateViewport(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                               Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                               MarginB + h2 * 0.5).Convert2D(0, -297 * i),
                                lst[1].GetCenter(), (int)scale);
                        }
                        else
                        {
                            double viwer1Width = lst[0].W() / scale;
                            double viwer1Height = lst[0].L() / scale;

                            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                         Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(0, -297 * i),
                         lst[0].GetCenter(), (int)scale);
                            #endregion

                            CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                                MarginB + h * 0.5).Convert2D(0, -297 * i),
                                lst[0].GetCenter(), (int)scale);
                        }
                    }
                    else
                    {
                      #region 第一页
                        double viwer1Width = lst[0].W() / scale;
                        double viwer1Height = lst[0].L() / scale;

                        #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                        CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                     Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(0, -297 * i),
                     lst[0].GetCenter(), (int)scale);
                        #endregion

                        CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                            Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                            MarginB + h * 0.5).Convert2D(0, -297 * i),
                            lst[0].GetCenter(), (int)scale);
                        #endregion

                        #region 第二页
                        if (lst.Count == 3)
                        {
                            viwer1Width = lst[1].W() / scale;
                            viwer1Height = lst[1].L() / scale;
                            double viwer2Height = lst[2].L() / scale;
                            double viwer2Width = lst[2].W() / scale;
                            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                         Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420, -297 * i),
                          lst[0].GetCenter(), (int)scale);
                            #endregion
                            double h1 = h * (viwer1Height) / (viwer1Height + viwer2Height);
                            double h2 = h * (viwer2Height) / (viwer1Height + viwer2Height);
                            CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                                MarginB + h2 + h1 * 0.5).Convert2D(420, -297 * i),
                                 lst[1].GetCenter(), (int)scale);

                            CreateViewport(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                               Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                               MarginB + h2 * 0.5).Convert2D(420, -297 * i),
                                lst[2].GetCenter(), (int)scale);
                            #endregion
                        }
                    }
                }
            
                trans.Commit();
            }
            //return ArrangeLayoutId;
        }

        /// <summary>
        /// 上下布局
        /// </summary>
        /// <param name="db"></param>
        /// <param name="layoutName"></param>
        /// <param name="ext"></param>
        /// <param name="extMerge"></param>
        /// <param name="scale"></param>
        /// <param name="pdfNum"></param>
        /// <param name="isA1"></param>
        /// <returns></returns>
        public static ObjectId CreateLayout(Database db, string layoutName, Extents2d ext, Extents2d extMerge,
            double scale,  int pdfNum = 1, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayout(layoutName, fsdTK, isA1, 1);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);

            double viwer1Width = ext.W() / scale;
            double viwer1Height = ext.L()  / scale;

            double viwer2Width = extMerge.W() / scale;
            double viwer2Height = extMerge.L()  / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB),
              ext.GetCenter(), (int)(scale));
            #endregion

            CreateViewport(db, ArrangeLayoutId, viwer1Width  + 3, viwer1Height  + 3,
                Point2d.Origin.Convert2D(w*0.5 + MarginL,
                MarginB+h* viwer2Height/(viwer1Height+ viwer2Height)+(h * viwer1Height / (viwer1Height + viwer2Height))*0.5),
                ext.GetCenter(), scale);

            CreateViewport(db, ArrangeLayoutId, viwer2Width  + 3, viwer2Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                 MarginB + (h * viwer2Height / (viwer1Height+viwer2Height))*0.5),
                extMerge.GetCenter().Convert2D(), scale );
            return ArrangeLayoutId;
        }

        public static ObjectId CreateCLayout(Database db, string layoutName, Extents2d ext, Extents2d extMerge,
         double scale, int pdfNum = 1,int pageNo=0, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, 1, pageNo);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);

            double viwer1Width = ext.W() / scale;
            double viwer1Height = ext.L() / scale;

            double viwer2Width = extMerge.W() / scale;
            double viwer2Height = extMerge.L() / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(0, -pageNo*297),
              ext.GetCenter(), (int)(scale));
            #endregion

            CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(0, -pageNo * 297),
                ext.GetCenter(), scale);

            CreateViewport(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                 MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(0, -pageNo * 297),
                extMerge.GetCenter().Convert2D(), scale);
            return ArrangeLayoutId;
        }
        public static ObjectId CreateLayoutNew(Database db, string layoutName, Extents2d ext, Extents2d extMerge,
          double scale, int pdfNum = 1, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayout(layoutName, fsdTK, isA1, 1);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);

            double viwer1Width = ext.W() * 1000 / scale;
            double viwer1Height = ext.L() * 1000 / scale;

            double viwer2Width = extMerge.W() * 1000 / scale;
            double viwer2Height = extMerge.L() * 1000 / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB),
              ext.GetCenter(), (int)(scale));
            #endregion

            CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5),
                ext.GetCenter(), scale / 1000);

            CreateViewport(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                 MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5),
                extMerge.GetCenter().Convert2D(), scale / 1000);
            return ArrangeLayoutId;
        }


        public static ObjectId CreatPlinthLayoutNew(Database db, string layoutName, Extents2d ext, Extents2d extMerge,
        double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;
            double bh = 50;

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT- bh);

            double viwer1Width = ext.W()*1000 / scale;
            double viwer1Height = ext.L() * 1000 / scale;

            double viwer2Width = extMerge.W() * 1000 / scale;
            double viwer2Height = extMerge.L() * 1000 / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewportNew(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
              ext.GetCenter(), (int)scale);
            #endregion
            //if (isDiv)
            //{
            //    if (!isLastDiv)
            //    {
            //        if (isFist)
            //        {
            //            //Point2d pt1 = ext.GetCenter31();
            //            //Point2d pt2 = ext.GetCenter();
            //            //Point2d pt3 = ext.GetCenter32();
            //            CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height + 3,
            //            Point2d.Origin.Convert2D(w * 0.5 + MarginL,
            //            bh + MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
            //            ext.GetCenter31(), scale / 1000);

            //            CreateViewportNew(db, ArrangeLayoutId, viwer2Width * 2 / 3 + 3, viwer2Height  + 3,
            //                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
            //                bh + MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
            //                extMerge.GetCenter31(), scale / 1000);
            //        }
            //        else
            //        {
            //            //Point2d pt1 = ext.GetCenter31();
            //            //Point2d pt2 = ext.GetCenter();
            //            //Point2d pt3 = ext.GetCenter32();
            //            CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height  + 3,
            //            Point2d.Origin.Convert2D(w * 0.5 + MarginL,
            //            bh + MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
            //            ext.GetCenter32(), scale / 1000);

            //            CreateViewportNew(db, ArrangeLayoutId, viwer2Width * 2 / 3 + 3, viwer2Height  + 3,
            //                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
            //                bh + MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
            //                extMerge.GetCenter32(), scale / 1000);
            //        }
            //    }
            //    else
            //    {

            //        CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height + 3,
            // Point2d.Origin.Convert2D(w * 0.5 + MarginL,
            // bh-10 + MarginB + h * (viwer2Height + viwer1Height) / (viwer1Height+ viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height+ viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
            // ext.GetCenter31(), scale / 1000);

            //        CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height + 3,
            //       Point2d.Origin.Convert2D(w * 0.5 + MarginL,
            //       bh-10 + MarginB + h * viwer2Height / (viwer1Height+ viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height+ viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
            //       ext.GetCenter32(), scale / 1000);


            //        CreateViewportNew(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
            //            Point2d.Origin.Convert2D(w * 0.5 + MarginL,
            //            bh-10 + MarginB + (h * viwer2Height / (viwer1Height+ viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
            //            extMerge.GetCenter().Convert2D(), scale / 1000);
            //    }
            //}
            //else
            //{
                CreateViewportNew(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    bh + MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    ext.GetCenter(), scale / 1000);

                CreateViewportNew(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    bh + MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    extMerge.GetCenter().Convert2D(), scale / 1000);
            //}
          
            return ArrangeLayoutId;
        }

        public static ObjectId CreatRampPlinthLayoutNew(Database db, string layoutName, Extents2d ext, Extents2d extA, Extents2d extB, Extents2d extC,
  double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;
            double bh = 0;

            double w = (420 - MarginL - MarginR)*2/3;
            double h = (297 - MarginB - MarginT - 8);

            double viwer1Width = ext.W() * 1000 / scale;
            double viwer1Height = ext.L() * 1000 / scale;

            double viwer2Width = extA.W() * 1000 / scale;
            double viwer2Height = extA.L() * 1000 / scale;

            double viwer3Width = extB.W() * 1000 / scale;
            double viwer3Height = extB.L() * 1000 / scale;

            double viwer4Width = extC.W() * 1000 / scale;
            double viwer4Height = extC.L() * 1000 / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewportNew(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
              ext.GetCenter(), (int)scale);
            #endregion

            CreateViewportNew(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL+10,
                bh + MarginB + h * (viwer2Height + viwer3Height) / (viwer1Height + viwer2Height + viwer3Height) + (h * viwer1Height / (viwer1Height + viwer2Height + viwer3Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                ext.GetCenter(), scale / 1000);

            CreateViewportNew(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
       Point2d.Origin.Convert2D(w * 0.5 + MarginL,
       bh + MarginB + h * viwer3Height / (viwer1Height + viwer2Height + viwer3Height) + (h * viwer2Height / (viwer1Height + viwer2Height + viwer3Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
       extA.GetCenter(), scale / 1000);

            CreateViewportNew(db, ArrangeLayoutId, viwer3Width + 3, viwer3Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                bh + MarginB + (h * viwer3Height / (viwer1Height + viwer2Height + viwer3Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                extB.GetCenter().Convert2D(), scale / 1000);

            CreateViewportNew(db, ArrangeLayoutId, viwer4Width + 3, viwer4Height + 3,
           Point2d.Origin.Convert2D(w+w * 0.25 + MarginL,
           50 + MarginB + (h) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
           extC.GetCenter().Convert2D(), scale / 1000);

            return ArrangeLayoutId;
        }

        public static ObjectId CreatPlinthLayoutNewBak(Database db, string layoutName, Extents2d ext, Extents2d extMerge,
     double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isDiv = false, bool isFist = true, bool isLastDiv = false, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, pdfNum, pageNo, divNo);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;
            double bh = 50;

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT - bh);

            double viwer1Width = ext.W() * 1000 / scale;
            double viwer1Height = ext.L() * 1000 / scale;

            double viwer2Width = extMerge.W() * 1000 / scale;
            double viwer2Height = extMerge.L() * 1000 / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewportNew(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
              ext.GetCenter(), (int)scale);
            #endregion
            if (isDiv)
            {
                if (!isLastDiv)
                {
                    if (isFist)
                    {
                        //Point2d pt1 = ext.GetCenter31();
                        //Point2d pt2 = ext.GetCenter();
                        //Point2d pt3 = ext.GetCenter32();
                        CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height + 3,
                        Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                        bh + MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                        ext.GetCenter31(), scale / 1000);

                        CreateViewportNew(db, ArrangeLayoutId, viwer2Width * 2 / 3 + 3, viwer2Height + 3,
                            Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                            bh + MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                            extMerge.GetCenter31(), scale / 1000);
                    }
                    else
                    {
                        //Point2d pt1 = ext.GetCenter31();
                        //Point2d pt2 = ext.GetCenter();
                        //Point2d pt3 = ext.GetCenter32();
                        CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height + 3,
                        Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                        bh + MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                        ext.GetCenter32(), scale / 1000);

                        CreateViewportNew(db, ArrangeLayoutId, viwer2Width * 2 / 3 + 3, viwer2Height + 3,
                            Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                            bh + MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                            extMerge.GetCenter32(), scale / 1000);
                    }
                }
                else
                {

                    CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height + 3,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL,
             bh - 10 + MarginB + h * (viwer2Height + viwer1Height) / (viwer1Height + viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter31(), scale / 1000);

                    CreateViewportNew(db, ArrangeLayoutId, viwer1Width * 2 / 3 + 3, viwer1Height + 3,
                   Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                   bh - 10 + MarginB + h * viwer2Height / (viwer1Height + viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                   ext.GetCenter32(), scale / 1000);


                    CreateViewportNew(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                        Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                        bh - 10 + MarginB + (h * viwer2Height / (viwer1Height + viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                        extMerge.GetCenter().Convert2D(), scale / 1000);
                }
            }
            else
            {
                CreateViewportNew(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    bh + MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    ext.GetCenter(), scale / 1000);

                CreateViewportNew(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                    Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                    bh + MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                    extMerge.GetCenter().Convert2D(), scale / 1000);
            }

            return ArrangeLayoutId;
        }
        public static ObjectId CreateLayout(Database db, string layoutName, Dictionary<int, List<Extents2d>> dic, 
      double scale, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayout(layoutName, fsdTK, isA1, dic.Count);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;
            Extents2d ext, extMerge;
            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);
            for (int i = 0; i < dic.Count; i++)
            {
                List<int> keys = dic.Keys.ToList();
                List<Extents2d> lst = dic[keys[i]];
                if (lst.Count > 1)
                {
                    ext = lst[0];
                    extMerge= lst[1];
                    double viwer1Width = ext.W() / scale;
                    double viwer1Height = ext.L() / scale;

                    double viwer2Width = extMerge.W() / scale;
                    double viwer2Height = extMerge.L() / scale;

                    #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                    CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                      Point2d.Origin.Convert2D(420*i+w * 0.5 + MarginL, MarginB),
                      ext.GetCenter(), (int)scale);
                    #endregion

                    CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                        Point2d.Origin.Convert2D(420 * i + w * 0.5 + MarginL,
                        MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5),
                        ext.GetCenter(), (int)scale);

                    CreateViewport(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                        Point2d.Origin.Convert2D(420 * i + w * 0.5 + MarginL,
                         MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5),
                        extMerge.GetCenter().Convert2D(), (int)scale);
                }
                else
                {
                    ext = lst[0];
                    double viwer1Width = ext.W() / scale;
                    double viwer1Height = ext.L() / scale;

                    #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                    CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                 Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * i,0),
                 ext.GetCenter(), (int)scale);
                    #endregion

                    CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                        Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                        MarginB + h * 0.5).Convert2D(420 * i, 0),
                        ext.GetCenter(), (int)scale);                    
                }
            }
            return ArrangeLayoutId;
        }

        public static ObjectId CreateLayoutNew(Database db, string layoutName, Dictionary<int, List<Extents2d>> dic,
   double scale, int pageNo = 0,  bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            //ArrangeLayoutId = db.CreatLayout(layoutName, fsdTK, isA1, dic.Count);
            ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, dic.Count, pageNo);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;
            Extents2d ext, extMerge;
            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);
            for (int i = 0; i < dic.Count; i++)
            {
                List<int> keys = dic.Keys.ToList();
                List<Extents2d> lst = dic[keys[i]];
                if (lst.Count > 1)
                {
                    ext = lst[0];
                    extMerge = lst[1];
                    double viwer1Width = ext.W() / scale;
                    double viwer1Height = ext.L() / scale;

                    double viwer2Width = extMerge.W() / scale;
                    double viwer2Height = extMerge.L() / scale;

                    #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                    CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                      Point2d.Origin.Convert2D(420 * i + w * 0.5 + MarginL, MarginB).Convert2D(0,-pageNo*297),
                      ext.GetCenter(), (int)scale);
                    #endregion

                    CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                        Point2d.Origin.Convert2D(420 * i + w * 0.5 + MarginL,
                        MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(0, -pageNo * 297),
                        ext.GetCenter(), (int)scale);

                    CreateViewport(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                        Point2d.Origin.Convert2D(420 * i + w * 0.5 + MarginL,
                         MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(0, -pageNo * 297),
                        extMerge.GetCenter().Convert2D(), (int)scale);
                }
                else
                {
                    ext = lst[0];
                    double viwer1Width = ext.W() / scale;
                    double viwer1Height = ext.L() / scale;

                    #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
                    CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
                 Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * i, 0).Convert2D(0, -pageNo * 297),
                 ext.GetCenter(), (int)scale);
                    #endregion

                    CreateViewport(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                        Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                        MarginB + h * 0.5).Convert2D(420 * i, 0).Convert2D(0, -pageNo * 297),
                        ext.GetCenter(), (int)scale);
                }
            }
            return ArrangeLayoutId;
        }

        public static ObjectId CreateLayoutNew(Database db, string layoutName, Extents2d ext, Extents2d extMerge,
        double scale, int pdfNum = 1, int pageNo = 0,int divNo=0, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, 1, pageNo,divNo);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);

            double viwer1Width = ext.W() / scale;
            double viwer1Height = ext.L() / scale;

            double viwer2Width = extMerge.W() / scale;
            double viwer2Height = extMerge.L() / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewportNew(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297* pageNo),
              ext.GetCenter(), (int)scale);
            #endregion

            CreateViewportNew(db, ArrangeLayoutId, viwer1Width + 3, viwer1Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                MarginB + h * viwer2Height / (viwer1Height + viwer2Height) + (h * viwer1Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                ext.GetCenter(), (int)scale);

            CreateViewportNew(db, ArrangeLayoutId, viwer2Width + 3, viwer2Height + 3,
                Point2d.Origin.Convert2D(w * 0.5 + MarginL,
                 MarginB + (h * viwer2Height / (viwer1Height + viwer2Height)) * 0.5).Convert2D(420 * divNo, -297 * pageNo),
                extMerge.GetCenter().Convert2D(), (int)scale);
            return ArrangeLayoutId;
        }

        /// <summary>
        /// 左右布局
        /// </summary>
        /// <param name="db"></param>
        /// <param name="layoutName"></param>
        /// <param name="ext"></param>
        /// <param name="extMerge"></param>
        /// <param name="scale"></param>
        /// <param name="pdfNum"></param>
        /// <param name="isA1"></param>
        /// <returns></returns>
        public static ObjectId CreateLayoutL2R(Database db, string layoutName, Extents2d ext, Extents2d extMerge,
                  double scale, int pdfNum = 1, int pageNo = 0, int divNo = 0, bool isA1 = false, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            string fsdTK = Extensions.curPaht + @"DWG\Title-DEEP T2&T3 (A3)_R3.dwg";
            if (File.Exists(tkPath))
                fsdTK = tkPath;
            ArrangeLayoutId = db.CreatLayoutNew(layoutName, fsdTK, isA1, 1, pageNo, divNo);

            double MarginL = 20;
            double MarginR = 5;
            double MarginB = 25;
            double MarginT = 7;

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);

            double viwer1Width = ext.W() / scale;
            double viwer1Height = ext.L() / scale;

            double viwer2Width = extMerge.W() / scale;
            double viwer2Height = extMerge.L() / scale;

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
             Point2d.Origin.Convert2D(w * 0.5 + MarginL, MarginB).Convert2D(420 * divNo, -297 * pageNo),
             ext.GetCenter(), (int)scale);
            #endregion

            CreateViewport(db, ArrangeLayoutId, viwer1Width  + 3, viwer1Height  + 3,
                Point2d.Origin.Convert2D(w*viwer1Width/(viwer1Width+ viwer2Width) * 0.5 + MarginL,
                MarginB+h/ 2).Convert2D(420 * divNo, -297 * pageNo),
                ext.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, viwer2Width / scale + 3, viwer2Height / scale + 3,
                Point2d.Origin.Convert2D( MarginL+ w * viwer1Width / (viwer1Width + viwer2Width) + w * viwer2Width / (viwer1Width + viwer2Width)*0.5,
                h/ 2).Convert2D(420 * divNo, -297 * pageNo),
                extMerge.GetCenter().Convert2D(), (int)scale);
            return ArrangeLayoutId;
        }
        /// <summary>
        /// 创建自动布局(4视口) ,完善中
        /// </summary>
        /// <param name="db"></param>
        /// <param name="layoutName"></param>
        /// <param name="extA">左上AA断面</param>
        /// <param name="extB">右上BB断面</param>
        /// <param name="extPlan">左下平面</param>
        /// <param name="extDetail">右下详图</param>
        /// <param name="refA">AA参考点（MS）</param>
        /// <param name="refB">BB参考点（MS）</param>
        /// <param name="refP">平面参考点（MS）</param>
        /// <param name="refD">大样参考点（MS）</param>
        /// <param name="MarginL">左侧页边距</param>
        /// <param name="MarginR">右侧及其他页边距</param>
        /// <returns></returns>
        public static ObjectId CreateLayoutAuto(Database db, string layoutName,
            Extents2d extA,Extents2d extB,Extents2d extPlan,Extents2d extDetail,
            Point2d refA, Point2d refB, Point2d refP, Point2d refD,
            double MarginL,double MarginR,double scale, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            //string tkPath = "";
            if(System.IO.File.Exists(Extensions.curTKPath))
            {
                tkPath = Extensions.curTKPath;
            }
            bool isA1 = false;
            if(Extensions.curMapSheet=="A1")
            {
                isA1 = true;
            }
            ArrangeLayoutId = db.CreatLayout(layoutName, tkPath, isA1, 1);

            double VPWidth1 = Math.Max(extA.W(), extPlan.W()) / scale;
            double VPHight1 = Math.Max(extA.L(), extB.L()) / scale;
            double M1 = (420-MarginL-MarginR - (VPWidth1 + extB.W() / scale) )/ 3.0;            
            double M2= (297 -2* MarginR - (VPHight1 + extB.L() / scale)) / 3.0;

            double M3 = (420 - MarginL - MarginR - (VPWidth1 + extPlan.W() / scale)) / 3.0;
            double M4 = (297 - 2 * MarginR - (VPHight1 + extDetail.L() / scale)) / 3.0;
          

            CreateViewport(db, ArrangeLayoutId, extA.W() / scale, extA.L() / scale,
                Point2d.Origin.Convert2D(MarginL+M1+0.5*VPWidth1,MarginR+M2+extPlan.L()/scale+M2+0.5*VPHight1),
                extA.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extB.W() / scale, extB.L() / scale,
                Point2d.Origin.Convert2D(MarginL + M1 + VPWidth1+M1+extB.W()/scale*0.5, MarginR + M2 + extPlan.L() / scale + M2 + 0.5 * VPHight1),
                extB.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extPlan.W() / scale, extPlan.L() / scale,
                Point2d.Origin.Convert2D(MarginL + M1 + VPWidth1*0.5 , MarginR + M2 + extPlan.L() / scale*0.5 ),
                extPlan.GetCenter(), (int)scale);


            CreateViewport(db, ArrangeLayoutId, extDetail.W() / scale, extDetail.L() / scale,
            Point2d.Origin.Convert2D(MarginL + M1 + VPWidth1 + M1 + extB.W() / scale * 0.5, MarginR + M4 + extDetail.L() / scale*0.5),
            extDetail.GetCenter(), (int)scale);

            return ArrangeLayoutId;
        }

        /// <summary>
        /// 创建自动布局(4视口) ,完善中
        /// </summary>
        /// <param name="db"></param>
        /// <param name="layoutName"></param>
        /// <param name="extA">左上AA断面</param>
        /// <param name="extB">右上BB断面</param>
        /// <param name="extPlan">左下平面</param>
        /// <param name="extDetail">右下详图</param>
        /// <param name="refA">AA参考点（MS）</param>
        /// <param name="refB">BB参考点（MS）</param>
        /// <param name="refP">平面参考点（MS）</param>
        /// <param name="refD">大样参考点（MS）</param>
        /// <param name="MarginL">左侧页边距</param>
        /// <param name="MarginR">右侧及其他页边距</param>
        /// <returns></returns>
        public static ObjectId CreateLayout(Database db, string layoutName,
            Extents2d extA, Extents2d extB, Extents2d extPlan, Extents2d extDetail,
            Point2d refA, Point2d refB, Point2d refP, Point2d refD,
            double MarginL, double MarginR, double MarginB, double MarginT, double scale, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            //string tkPath = "";
            if (System.IO.File.Exists(Extensions.curTKPath))
            {
                tkPath = Extensions.curTKPath;
            }
            bool isA1 = false;
            if (Extensions.curMapSheet == "A1")
            {
                isA1 = true;
            }
            ArrangeLayoutId = db.CreatLayout(layoutName, tkPath, isA1, 1);

            double VPWidth1 = Math.Max(extA.W(), extPlan.W()) / scale;
            double VPHight1 = Math.Max(extA.L(), extB.L()) / scale;
            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);

            double wA = w * (extA.W() / (extA.W() + extB.W()));  //A视口长度
            double wB = w * (extB.W() / (extA.W() + extB.W()));  //B视口长度
            double hA = h * (extA.L() / (extA.L() + extPlan.L()));  //A视口高度
            double hP = h * (extPlan.L() / (extA.L() + extPlan.L()));  //Plan视口高度
          
            double M1 = MarginL+wA;  //B视口位置
            double M2 = MarginB + hP;  //B视口位置


            CreateViewport(db, ArrangeLayoutId, extA.W() / scale + 5, extA.L() / scale + 5,
                Point2d.Origin.Convert2D(MarginL+ wA*0.5, M2 + 0.5 * hA),
                extA.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extB.W() / scale + 5, extB.L() / scale + 5,
                Point2d.Origin.Convert2D(M1 + wB * 0.5, M2 + 0.5 * hA),
                extB.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extPlan.W() / scale + 5, extPlan.L() / scale + 5,
                Point2d.Origin.Convert2D(MarginL+0.5* wA, MarginB+0.5* hP),
                extPlan.GetCenter(), (int)scale);


            CreateViewport(db, ArrangeLayoutId, extDetail.W() / scale + 5, extDetail.L() / scale+5,
            Point2d.Origin.Convert2D(M1  +wB * 0.5, MarginB+0.5 * hP),
            extDetail.GetCenter(), (int)scale);

            return ArrangeLayoutId;
        }

        public static ObjectId CreateLayout3Div(Database db, string layoutName,
                 Extents2d extE, Extents2d extP, Extents2d extDetail,
                 Point2d refE, Point2d refP, Point2d refD,
                 double MarginL, double MarginR, double MarginB, double MarginT, double scale, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            //string tkPath = "";
            if (System.IO.File.Exists(Extensions.curTKPath))
            {
                tkPath = Extensions.curTKPath;
            }
            bool isA1 = false;
            if (Extensions.curMapSheet == "A1")
            {
                isA1 = true;
            }
            ArrangeLayoutId = db.CreatLayout(layoutName, tkPath, isA1, 1);
            double VPWidth1 = extE.W() / scale;    
            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);
            double hA = h * (extE.L() / (extE.L() + extP.L()+ extDetail.L()));  //A视口高度
            double hP = h * (extP.L() / (extE.L() + extP.L() + extDetail.L()));  //Plan视口高度
            double hD = h * (extDetail.L() / (extE.L() + extP.L() + extDetail.L()));  //Plan视口高度

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(),
              extE.GetCenter(), (int)scale);
            #endregion
            CreateViewport(db, ArrangeLayoutId, extE.W() / scale + 5, extE.L() / scale + 5,
                Point2d.Origin.Convert2D(MarginL + w * 0.5, MarginB + hD+hP+ 0.5 * hA),
                extE.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extP.W() / scale + 5, extP.L() / scale + 5,
                Point2d.Origin.Convert2D(MarginL + w * 0.5, MarginB + hD + 0.5 * hP),
                extP.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extDetail.W() / scale + 5, extDetail.L() / scale + 5,
                Point2d.Origin.Convert2D(MarginL + 0.5 * w, MarginB + 0.5 * hD),
                extDetail.GetCenter(), (int)scale);

            return ArrangeLayoutId;
        }

        public static ObjectId CreatePileLayout(Database db, string layoutName,
        Extents2d extA, Extents2d extB, Extents2d extPlan, Extents2d extDetail,
        Point2d refA, Point2d refB, Point2d refP, Point2d refD,
        double MarginL, double MarginR, double MarginB, double MarginT, double scale, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            //string tkPath = "";
            if (System.IO.File.Exists(Extensions.curTKPath))
            {
                tkPath = Extensions.curTKPath;
            }
            bool isA1 = false;
            if (Extensions.curMapSheet == "A1")
            {
                isA1 = true;
            }
            ArrangeLayoutId = db.CreatLayout(layoutName, tkPath, isA1, 1);

            double w = (420 - MarginL - MarginR);
            double h = (297 - MarginB - MarginT);
            double HA = extA.L()/ scale;
            double HB = extB.L() / scale;
            double wA = extA.W() / scale;  //A视口长度
            double wB = extB.W() / scale;  //B视口长度
            double wC = w * 0.5;  //B视口长度
            double M1 = MarginL + wA+10;  //B视口位置
            double M2 = MarginB ;  //B视口位置

            #region 无效视口方便自动保存，不加这一个自动保存数据视口小时
            CreateViewport(db, ArrangeLayoutId, 0.1, 0.1,
              Point2d.Origin.Convert2D(),
              extA.GetCenter(), (int)scale);
            #endregion

            CreateViewport(db, ArrangeLayoutId, extA.W() / scale+5, extA.L() / scale + 5,
                Point2d.Origin.Convert2D(MarginL + wA * 0.5, M2+0.5* HA),
                extA.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extB.W() / scale + 5, extB.L() / scale + 5,
                Point2d.Origin.Convert2D(M1 + wB*0.5, M2 + 0.5* HB),
                extB.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extPlan.W() / scale+5, extPlan.L() / scale+5,
                Point2d.Origin.Convert2D(M1 + extPlan.W() / scale * 0.5, M2 + HB+(h-HB)*0.5),
                extPlan.GetCenter(), (int)scale);


         CreateViewport(db, ArrangeLayoutId, extDetail.W() / scale + 5, extDetail.L() / scale+5,
            Point2d.Origin.Convert2D(M1  +wB+ 5 + extDetail.W()*0.5 / scale, MarginB+0.75 * h*0.5),
            extDetail.GetCenter(), (int)scale);
            //CreateViewport(db, ArrangeLayoutId, extDetail.W() / scale, extDetail.L() / scale,
            //Point2d.Origin.Convert2D(MarginL + MarginB*3+ extA.W() / scale+ extB.W() / scale + extDetail.L() / scale * 0.5,
            //M2 + 0.5 * HB),
            //extDetail.GetCenter(), (int)scale);

            return ArrangeLayoutId;
        }

        public static ObjectId CreatePileLayoutAuto(Database db, string layoutName,
            Extents2d extA, Extents2d extB, Extents2d extPlan, 
            Point2d refA, Point2d refB, Point2d refP,
            double MarginL, double MarginR, double scale, string tkPath = "")
        {
            ObjectId ArrangeLayoutId;
            //string tkPath = "";
            if (System.IO.File.Exists(Extensions.curTKPath))
            {
                tkPath = Extensions.curTKPath;
            }
            bool isA1 = false;
            if (Extensions.curMapSheet == "A1")
            {
                isA1 = true;
            }
            ArrangeLayoutId = db.CreatLayout(layoutName, tkPath, isA1, 1);

            double VPWidth1 = Math.Max(extA.W(), extPlan.W()) / scale;
            double VPHight1 = Math.Max(extA.L(), extB.L()) / scale;
            double M1 = (420 - MarginL - MarginR - (VPWidth1 + extB.W() / scale)) / 3.0;
            double M2 = (297 - 2 * MarginR - (VPHight1 + extB.L() / scale)) / 3.0;

            double M3 = (420 - MarginL - MarginR - (VPWidth1 + extPlan.W() / scale)) / 3.0;
          
            CreateViewport(db, ArrangeLayoutId, extA.W() / scale, extA.L() / scale,
                Point2d.Origin.Convert2D(MarginL + M1 + 0.3 * VPWidth1, MarginR + M2 + extPlan.L() / scale + M2 + 1 * VPHight1),
                extA.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extB.W() / scale, extB.L() / scale,
                Point2d.Origin.Convert2D(MarginL + M1 + VPWidth1 + M1 + extB.W() / scale * 0.5, MarginR + M2 + extPlan.L() / scale + M2 + 1* VPHight1),
                extB.GetCenter(), (int)scale);

            CreateViewport(db, ArrangeLayoutId, extPlan.W() / scale, extPlan.L() / scale,
                Point2d.Origin.Convert2D(MarginL + M1 + VPWidth1 * 0.5, MarginR + M2 + extPlan.L() / scale + M2 + 1* VPHight1),
                extPlan.GetCenter(), (int)scale);

          
            return ArrangeLayoutId;
        }
        public static void CreateViewport(Database db ,ObjectId LayoutId, double W,double L,Point2d PSCenter,Point2d MSCenter,double scale)
        {
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                Layout lay = (Layout)tr.GetObject(LayoutId, OpenMode.ForWrite);
                UcsTable ut = tr.GetObject(db.UcsTableId, OpenMode.ForWrite) as UcsTable;
                var btr = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite);

                // 平面视口
                Viewport vpFlat = new Viewport();
                btr.AppendEntity(vpFlat);
                tr.AddNewlyCreatedDBObject(vpFlat, true);
                vpFlat.Layer = "图框";
                vpFlat.On = true;
                vpFlat.GridOn = false;
                vpFlat.SettingSizeAndLoc(W,L, PSCenter,scale);
                vpFlat.ViewCenter = MSCenter;
                //vpFlat.Visible = true;
                //vpFlat.Locked = true;
                vpFlat.Visible = false;
                vpFlat.Locked = false;
                //viewCenter = vpFlat.ViewCenter;
                //vpFlat.ViewCenter = new Point2d((pt.X+ offset+ offsetY + areaWidth) /2, (pt.Y + areaHeight) /2);
                tr.Commit();
            }
            return;
        }

        public static void CreateViewportByPointList(Database db, ObjectId LayoutId, double W, double L, List<Point2d> ptLst, Point2d PSCenter, double scale)
        {
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                Layout lay = (Layout)tr.GetObject(LayoutId, OpenMode.ForWrite);
                UcsTable ut = tr.GetObject(db.UcsTableId, OpenMode.ForWrite) as UcsTable;
                var btr = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite);
                
              
                // 平面视口
                Viewport vpFlat = new Viewport();
                btr.AppendEntity(vpFlat);
                tr.AddNewlyCreatedDBObject(vpFlat, true);
                vpFlat.Layer = "图框";
                vpFlat.On = true;
                vpFlat.GridOn = false;
                vpFlat.SettingSizeAndLoc(W, L, PSCenter, scale);
                
                UcsTableRecord ucs2;
                if (!ut.Has("PlanUCS"))
                {
                    ucs2 = new UcsTableRecord();
                    ucs2.Name = "PlanUCS";
                    ucs2.Origin = ptLst[0].Convert3D();

                    ucs2.XAxis = (ptLst[3] - ptLst[2]).Convert3D();
                    ucs2.YAxis = (ptLst[3] - ptLst[2]).Convert3D().RotateBy(0.5 * Math.PI, Vector3d.ZAxis);

                    ut.Add(ucs2);
                    tr.AddNewlyCreatedDBObject(ucs2, true);
                }
                else
                {
                    ucs2 = tr.GetObject(ut["PlanUCS"], OpenMode.ForWrite) as UcsTableRecord;
                    ucs2.Name = "PlanUCS";
                    ucs2.Origin = ptLst[0].Convert3D();

                    ucs2.XAxis = (ptLst[3] - ptLst[2]).Convert3D();
                    ucs2.YAxis = (ptLst[3] - ptLst[2]).Convert3D().RotateBy(0.5 * Math.PI, Vector3d.ZAxis);
         
                }
                //UcsTableRecord ucs = tr.GetObject(UcsId, OpenMode.ForRead) as UcsTableRecord;
                double twa2 = ucs2.XAxis.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis);
                double ang = GeTools.RadianToDegree(twa2);
                vpFlat.TwistAngle = twa2;

                Matrix2d TransX = Matrix2d.Rotation(twa2, Point2d.Origin);
                Point2d MSCenter;
                if (ang>45)
                   MSCenter = GeTools.MidPoint(ptLst[0].Convert3D(), ptLst[1].Convert3D()).Convert2D(20);
                else
                    MSCenter = GeTools.MidPoint(ptLst[0].Convert3D(), ptLst[1].Convert3D()).Convert2D(0,20);
                vpFlat.ViewCenter = MSCenter.TransformBy(TransX);

                vpFlat.Visible = true;
                vpFlat.Locked = false;

                tr.Commit();
            }
            return;
        }
        public static void CreateViewportNew(Database db, ObjectId LayoutId, double W, double L, Point2d PSCenter, Point2d MSCenter, double scale)
        {
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                Layout lay = (Layout)tr.GetObject(LayoutId, OpenMode.ForWrite);
                UcsTable ut = tr.GetObject(db.UcsTableId, OpenMode.ForWrite) as UcsTable;
                var btr = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite);

                // 平面视口
                Viewport vpFlat = new Viewport();
                btr.AppendEntity(vpFlat);
                tr.AddNewlyCreatedDBObject(vpFlat, true);
                vpFlat.Layer = "图框";
                vpFlat.On = true;
                vpFlat.GridOn = false;
                vpFlat.SettingSizeAndLoc(W, L, PSCenter, scale);
                vpFlat.ViewCenter = MSCenter;
                vpFlat.Visible = false;
                //vpFlat.Locked = true;

                //viewCenter = vpFlat.ViewCenter;
                //vpFlat.ViewCenter = new Point2d((pt.X+ offset+ offsetY + areaWidth) /2, (pt.Y + areaHeight) /2);
                tr.Commit();
            }
            return;
        }
        // public void ControlViewport()
        public static string GetPierHeightDes(string footingName, eSubType type)
        {
            int index = (int)type;
            if (type == eSubType.BM)
                index = 2;
            if (type == eSubType.CM)
                index = 3;
            int interval = 0;
            _ListFootingDes.TryGetValue(footingName.Replace("-",""), out interval);
           
            string des = "";
            if (footingName == "FPTM2U")
            {
                des = "20.00 M.Max";
            }
            else if (footingName == "F1PME5"|| footingName == "F2PME5")
            {
                if (type == eSubType.A|| type == eSubType.B)
                    des = "7.00~15.00m";
                else
                    des = string.Format("{0}.01~{1}.00m", interval * index, interval * (index + 1));
            }
            else if (footingName != "FPTM2S")
            {
                if (index == 1)
                {
                    if (interval == 5)
                        des = string.Format("{0}.00~{1}.00m", 7, interval * (index + 1));
                    else
                        des = string.Format("{0}.00~{1}.00m", interval * index, interval * (index + 1));
                }
                else
                {
                    des = string.Format("{0}.01~{1}.00m", interval * index, interval * (index + 1));
                }
            }
            else
            {
                if (type == eSubType.A)
                    des = "7.00~10.00m";
                else 
                   des = "20.00 M.Max";
            }
            return des;
        }

        public static string GetVarName<T>(System.Linq.Expressions.Expression<Func<T, T>> exp)
        {
            return ((System.Linq.Expressions.MemberExpression)exp.Body).Member.Name;
        }
    }
}
